import 'dart:developer';

import 'package:get/get.dart';
import 'package:ball_lion/entity/match/odds_company_entity.dart';
import 'package:ball_lion/entity/other/config_entity.dart';
import 'package:ball_lion/server/api/other_api.dart';
import 'package:ball_lion/server/http/request_header.dart';
import 'package:ball_lion/utils/sp_utils.dart';

import 'package:ball_lion/utils/user.dart';

class ConfigService extends GetxService {
  late ZqConfig zqConfig;
  late LqConfig lqConfig;
  late PushConfig pushConfig;
  int configTime = 0;
  String userId = '';
  int userType = 1; //0 userId, 1 uuid

  bool tipEnable = true;

  List<String> zqOddsType = ['欧指', '亚指', '进球数'];
  List<String> lqOddsType = ['胜负', '让分胜负', '大小分'];
  List<OddsCompanyEntity> oddsCompany = [];
  // List<String> oddsCompany = ['澳门', '皇冠', '易胜博', '韦德', '立博', 'Bet365'];
  List<String> soundName = ['哨子', '口哨', '鼓声', '号角', '胜利'];
  List zqSoundFile = [
    'shao_zi',
    'kou_shao',
    'gu_sheng',
    'hao_jiao',
    'sheng_li'
  ];
  Future<ConfigService> init() async {
    return this;
  }

  @override
  void onReady() async {
    getOddsCompany();

    super.onReady();
  }

  getOddsCompany() async {
    try {
      List<OddsCompanyEntity>? company = await OtherApi.getOddsCompany();
      oddsCompany = company ?? [];
    } catch (e) {}
  }

  loadConfig() async {
    if (oddsCompany.isEmpty) {
      await getOddsCompany();
    }
    if (User.auth != null) {
      userType = 0;
      userId = User.auth!.userId ?? '';
    }
    if (configTime == 0) {
      zqConfig = SpUtils.soccerConfig ?? ZqConfig();
      lqConfig = SpUtils.basketConfig ?? LqConfig();
      pushConfig = SpUtils.pushConfig ?? PushConfig();
      configTime = SpUtils.configTime ?? 0;
    }

    if (User.auth != null) {
      List<ConfigEntity>? config = await OtherApi.getConfigList([]);
      if (config != null) {
        Map<int, String> json = {};
        int updateTime = 0;
        for (var element in config) {
          json[element.type!] = element.config!;
          if (element.time! > updateTime) {
            updateTime = element.time!;
          }
        }
        if (updateTime > configTime) {
          zqConfig = ZqConfig.fromJson(json);
          lqConfig = LqConfig.fromJson(json);
          pushConfig = PushConfig.fromJson(json);
          configTime = updateTime;
        } else if (updateTime < configTime || updateTime == 0) {
          updateAll();
        }
      }
    }
  }

  saveConfigToLocal() {
    try {
      SpUtils.soccerConfig = zqConfig;
      SpUtils.basketConfig = lqConfig;
      SpUtils.pushConfig = pushConfig;
      SpUtils.configTime = configTime;
    } catch (e) {
      log('save config: $e');
    }
  }

  //同步本地配置到服务器
  updateAll() {
    Map<int, String> json = zqConfig.toJson();
    json.addAll(lqConfig.toJson());
    json.addAll(pushConfig.toJson());
    List<ConfigEntity> list = [];
    for (var element in json.keys) {
      list.add(
          ConfigEntity(config: json[element], type: element, typeName: ''));
    }
    OtherApi.setConfigList(list, userId, userType);
  }

  update(int id, dynamic data) {
    if (userId.isEmpty) {
      userId = RequestHeader.uuid;
    }
    String config = '';
    if (data is List) {
      config = data.join(',');
    } else if (data is int) {
      config = data.toString();
    }
    ConfigEntity entity = ConfigEntity(type: id, config: config, typeName: '');
    if (data is ConfigEntity) {
      entity = data;
    }
    configTime = DateTime.now().millisecondsSinceEpoch;
    log('${DateTime.now().millisecondsSinceEpoch}');
    OtherApi.setConfigList([entity], userId, userType);
  }

  logout() {
    userId = RequestHeader.uuid;
    userType = 1;
  }
}

//全局设置类
class PushConfig with Convert {
  int pushAll = 1;
  int pushNews = 1; //新闻资讯
  int pushExpert = 1; //关注专家提醒
  int pushInNight = 1; //夜间免打扰模式
  int bottomVibration = 1; //底部导航栏震动
  int liveBackground = 1; //后台播放

  PushConfig();

  PushConfig.fromJson(Map<int, String> data) {
    Map<int, dynamic> json = convertJson(data);
    if (json[ConfigType.pushAll] != null) {
      pushAll = json[ConfigType.pushAll];
    }
    if (json[ConfigType.pushNews] != null) {
      pushNews = json[ConfigType.pushNews];
    }
    if (json[ConfigType.pushExpert] != null) {
      pushExpert = json[ConfigType.pushExpert];
    }
    if (json[ConfigType.pushInNight] != null) {
      pushInNight = json[ConfigType.pushInNight];
    }
    if (json[ConfigType.bottomVibration] != null) {
      bottomVibration = json[ConfigType.bottomVibration];
    }
    if (json[ConfigType.liveBackground] != null) {
      bottomVibration = json[ConfigType.liveBackground];
    }
  }

  Map<int, String> toJson() {
    final Map<int, dynamic> data = new Map<int, dynamic>();
    data[ConfigType.pushAll] = pushAll;
    data[ConfigType.pushNews] = pushNews;
    data[ConfigType.pushExpert] = pushExpert;
    data[ConfigType.pushInNight] = pushInNight;
    data[ConfigType.bottomVibration] = bottomVibration;
    data[ConfigType.liveBackground] = liveBackground;
    Map<int, String> json = Map<int, String>();
    for (var key in data.keys) {
      if (data[key] is int) {
        json[key] = data[key].toString();
      } else if (data[key] is String) {
        json[key] = data[key];
      }
    }
    return json;
  }

  Map<String, dynamic> toStringJson() {
    Map<int, dynamic> data = toJson();
    Map<String, dynamic> json = {};
    for (var element in data.keys) {
      json[element.toString()] = data[element];
    }
    return json;
  }
}

class ZqConfig with Convert {
  int zqAlert1 = 1; //提醒范围
  List zqAlert2 = [0]; //进球提示
  int zqAlert3 = 4; //主队进球声音
  int zqAlert4 = 3; //客队进球声音
  List zqAlert5 = [0]; //红牌提示
  int zqAlert6 = 1; //弹窗范围
  int zqAlert7 = 1; //共享筛选

  int zqPush1 = 1; //关注
  int zqPush2 = 1; //首发阵容
  int zqPush3 = 1; //开赛
  int zqPush4 = 1; //赛果
  int zqPush5 = 1; //进球
  int zqPush6 = 1; //红牌
  int zqPush7 = 0; //黄牌
  int zqPush8 = 0; //角球

  //列表数据
  int zqList1 = 1; //指数
  int zqList2 = 1; //球队排名
  int zqList3 = 1; //彩种编号
  int zqList4 = 1; //方案
  int zqList5 = 1; //红黄牌
  int zqList6 = 1; //角球
  List zqList7 = [0, 1, 2]; //指数类型
  int zqList8 = Get.find<ConfigService>().oddsCompany.isEmpty
      ? 1
      : Get.find<ConfigService>()
              .oddsCompany
              .firstWhere((element) => element.d == 1)
              .id ??
          1; //指数公司
  int zqList9 = 1; //指数显示

  ZqConfig();

  ZqConfig.fromJson(Map<int, String> data) {
    Map<int, dynamic> json = convertJson(data);

    if (json[ConfigType.zqAlert1] != null) {
      zqAlert1 = json[ConfigType.zqAlert1];
    }
    if (json[ConfigType.zqAlert2] != null) {
      zqAlert2 = json[ConfigType.zqAlert2];
    }
    if (json[ConfigType.zqAlert3] != null) {
      zqAlert3 = json[ConfigType.zqAlert3];
    }
    if (json[ConfigType.zqAlert4] != null) {
      zqAlert4 = json[ConfigType.zqAlert4];
    }
    if (json[ConfigType.zqAlert5] != null) {
      zqAlert5 = json[ConfigType.zqAlert5];
    }
    if (json[ConfigType.zqAlert6] != null) {
      zqAlert6 = json[ConfigType.zqAlert6];
    }
    if (json[ConfigType.zqAlert7] != null) {
      zqAlert7 = json[ConfigType.zqAlert7];
    }
    if (json[ConfigType.zqPush1] != null) {
      zqPush1 = json[ConfigType.zqPush1];
    }
    if (json[ConfigType.zqPush2] != null) {
      zqPush2 = json[ConfigType.zqPush2];
    }
    if (json[ConfigType.zqPush3] != null) {
      zqPush3 = json[ConfigType.zqPush3];
    }
    if (json[ConfigType.zqPush4] != null) {
      zqPush4 = json[ConfigType.zqPush4];
    }
    if (json[ConfigType.zqPush5] != null) {
      zqPush5 = json[ConfigType.zqPush5];
    }
    if (json[ConfigType.zqPush6] != null) {
      zqPush6 = json[ConfigType.zqPush6];
    }
    if (json[ConfigType.zqPush7] != null) {
      zqPush7 = json[ConfigType.zqPush7];
    }
    if (json[ConfigType.zqPush8] != null) {
      zqPush8 = json[ConfigType.zqPush8];
    }
    if (json[ConfigType.zqList1] != null) {
      zqList1 = json[ConfigType.zqList1];
    }
    if (json[ConfigType.zqList2] != null) {
      zqList2 = json[ConfigType.zqList2];
    }
    if (json[ConfigType.zqList3] != null) {
      zqList3 = json[ConfigType.zqList3];
    }
    if (json[ConfigType.zqList4] != null) {
      zqList4 = json[ConfigType.zqList4];
    }
    if (json[ConfigType.zqList5] != null) {
      zqList5 = json[ConfigType.zqList5];
    }
    if (json[ConfigType.zqList6] != null) {
      zqList6 = json[ConfigType.zqList6];
    }
    if (json[ConfigType.zqList7] != null) {
      zqAlert2 = json[ConfigType.zqAlert2];
      // String str = json[ConfigType.soccerList7];
      // if (str.isEmpty) {
      //   soccerList7 = [];
      // } else {
      //   soccerList7 = str.split(',').map((e) => int.parse(e)).toList();
      // }
    }
    if (json[ConfigType.zqList8] != null) {
      zqList8 = json[ConfigType.zqList8];
    }
    if (json[ConfigType.zqList9] != null) {
      zqList9 = json[ConfigType.zqList9];
    }
  }

  Map<int, String> toJson() {
    final Map<int, dynamic> data = new Map<int, dynamic>();
    data[ConfigType.zqAlert1] = zqAlert1;
    data[ConfigType.zqAlert2] = zqAlert2.join(',');
    data[ConfigType.zqAlert3] = zqAlert3;
    data[ConfigType.zqAlert4] = zqAlert4;
    data[ConfigType.zqAlert5] = zqAlert5.join(',');
    data[ConfigType.zqAlert6] = zqAlert6;
    data[ConfigType.zqAlert7] = zqAlert7;
    data[ConfigType.zqPush1] = zqPush1;
    data[ConfigType.zqPush2] = zqPush2;
    data[ConfigType.zqPush3] = zqPush3;
    data[ConfigType.zqPush4] = zqPush4;
    data[ConfigType.zqPush5] = zqPush5;
    data[ConfigType.zqPush6] = zqPush6;
    data[ConfigType.zqPush7] = zqPush7;
    data[ConfigType.zqPush8] = zqPush8;
    data[ConfigType.zqList1] = zqList1;
    data[ConfigType.zqList2] = zqList2;
    data[ConfigType.zqList3] = zqList3;
    data[ConfigType.zqList4] = zqList4;
    data[ConfigType.zqList5] = zqList5;
    data[ConfigType.zqList6] = zqList6;
    data[ConfigType.zqList7] = zqList7.join(',');
    data[ConfigType.zqList8] = zqList8;
    data[ConfigType.zqList9] = zqList9;
    Map<int, String> json = Map<int, String>();
    for (var key in data.keys) {
      if (data[key] is int) {
        json[key] = data[key].toString();
      } else if (data[key] is String) {
        json[key] = data[key];
      }
    }
    return json;
  }

  Map<String, String> toStringJson() {
    Map<int, String> data = toJson();
    Map<String, String> json = {};
    for (var element in data.keys) {
      json[element.toString()] = data[element] ?? '';
    }
    return json;
  }

  String getPushConfigCount() {
    int count = 0;
    if (zqPush2 == 1) {
      count++;
    }
    if (zqPush3 == 1) {
      count++;
    }
    if (zqPush4 == 1) {
      count++;
    }
    if (zqPush5 == 1) {
      count++;
    }
    if (zqPush6 == 1) {
      count++;
    }
    if (zqPush7 == 1) {
      count++;
    }
    if (zqPush8 == 1) {
      count++;
    }
    return '$count/7';
  }
}

class LqConfig with Convert {
  //篮球推送
  int lqPush1 = 1; //关注
  int lqPush2 = 1; //开赛
  int lqPush3 = 1; //赛果

  //应用内提醒
  int lqAlert1 = 1; //提醒范围
  List lqAlert2 = [0]; //进球提示
  int lqAlert3 = 1; //弹窗范围
  int lqAlert4 = 1; //共享筛选

  //列表数据
  int lqList1 = 1; //指数
  int lqList2 = 1; //球队排名
  int lqList3 = 1; //彩种编号
  List lqList7 = [0, 1, 2]; //指数类型
  int lqList8 = Get.find<ConfigService>().oddsCompany.isEmpty
      ? 1
      : Get.find<ConfigService>()
              .oddsCompany
              .firstWhere((element) => element.d == 1)
              .id ??
          1; //指数公司
  int lqList9 = 1; //指数显示

  LqConfig();

  LqConfig.fromJson(Map<int, String> data) {
    Map<int, dynamic> json = convertJson(data);
    if (json[ConfigType.lqAlert1] != null) {
      lqAlert1 = json[ConfigType.lqAlert1];
    }
    if (json[ConfigType.lqAlert2] != null) {
      lqAlert2 = json[ConfigType.lqAlert2];
    }
    if (json[ConfigType.lqAlert3] != null) {
      lqAlert3 = json[ConfigType.lqAlert3];
    }
    if (json[ConfigType.lqAlert4] != null) {
      lqAlert4 = json[ConfigType.lqAlert4];
    }
    if (json[ConfigType.lqPush1] != null) {
      lqPush1 = json[ConfigType.lqPush1];
    }
    if (json[ConfigType.lqPush2] != null) {
      lqPush2 = json[ConfigType.lqPush2];
    }
    if (json[ConfigType.lqPush3] != null) {
      lqPush3 = json[ConfigType.lqPush3];
    }
    if (json[ConfigType.lqList1] != null) {
      lqList1 = json[ConfigType.lqList1];
    }
    if (json[ConfigType.lqList2] != null) {
      lqList2 = json[ConfigType.lqList2];
    }
    if (json[ConfigType.lqList3] != null) {
      lqList3 = json[ConfigType.lqList3];
    }
    if (json[ConfigType.lqList7] != null) {
      lqList7 = json[ConfigType.lqList7];
    }
    if (json[ConfigType.lqList8] != null) {
      lqList8 = json[ConfigType.lqList8];
    }
    if (json[ConfigType.lqList9] != null) {
      lqList9 = json[ConfigType.lqList9];
    }
  }

  Map<int, String> toJson() {
    final Map<int, dynamic> data = new Map<int, dynamic>();
    data[ConfigType.lqAlert1] = lqAlert1;
    data[ConfigType.lqAlert2] = lqAlert2.join(',');
    data[ConfigType.lqAlert3] = lqAlert3;
    data[ConfigType.lqAlert4] = lqAlert4;
    data[ConfigType.lqPush1] = lqPush1;
    data[ConfigType.lqPush2] = lqPush2;
    data[ConfigType.lqPush3] = lqPush3;
    data[ConfigType.lqList1] = lqList1;
    data[ConfigType.lqList2] = lqList2;
    data[ConfigType.lqList3] = lqList3;
    data[ConfigType.lqList7] = lqList7.join(',');
    data[ConfigType.lqList8] = lqList8;
    data[ConfigType.lqList9] = lqList9;
    Map<int, String> json = Map<int, String>();
    for (var key in data.keys) {
      if (data[key] is int) {
        json[key] = data[key].toString();
      } else if (data[key] is String) {
        json[key] = data[key];
      }
    }
    return json;
  }

  Map<String, dynamic> toStringJson() {
    Map<int, dynamic> data = toJson();
    Map<String, dynamic> json = {};
    for (var element in data.keys) {
      json[element.toString()] = data[element];
    }
    return json;
  }

  String getPushConfigCount() {
    int count = 0;
    if (lqPush2 == 1) {
      count++;
    }
    if (lqPush3 == 1) {
      count++;
    }
    return '$count/2';
  }
}

class ConfigType {
  static int pushAll = 0;
  static int pushNews = 1; //新闻资讯
  static int pushExpert = 2; //关注专家提醒
  static int pushInNight = 3; //夜间免打扰模式
  static int bottomVibration = 101; //底部导航栏震动
  static int liveBackground = 201; //后台播放

  //足球推送
  static int zqPush1 = 1001; //关注
  static int zqPush2 = 1002; //首发阵容
  static int zqPush3 = 1003; //开赛
  static int zqPush4 = 1004; //赛果
  static int zqPush5 = 1005; //进球
  static int zqPush6 = 1006; //红牌
  static int zqPush7 = 1007; //黄牌
  static int zqPush8 = 1008; //角球

  //应用内提醒
  static int zqAlert1 = 1101; //提醒范围
  static int zqAlert2 = 1102; //进球提示
  static int zqAlert3 = 1103; //主队进球声音
  static int zqAlert4 = 1104; //客队进球声音
  static int zqAlert5 = 1105; //红牌提示
  static int zqAlert6 = 1106; //弹窗范围
  static int zqAlert7 = 1107; //共享筛选

  //列表数据
  static int zqList1 = 1201; //指数
  static int zqList2 = 1202; //球队排名
  static int zqList3 = 1203; //彩种编号
  static int zqList4 = 1204; //方案
  static int zqList5 = 1205; //红黄牌
  static int zqList6 = 1206; //角球
  static int zqList7 = 1207; //指数类型
  static int zqList8 = 1208; //指数公司
  static int zqList9 = 1209; //指数显示

  //篮球推送
  static int lqPush1 = 2001; //关注
  static int lqPush2 = 2002; //开赛
  static int lqPush3 = 2003; //赛果

  //应用内提醒
  static int lqAlert1 = 2101; //提醒范围
  static int lqAlert2 = 2102; //进球提示
  static int lqAlert3 = 2103; //弹窗范围
  static int lqAlert4 = 2104; //共享筛选

  //列表数据
  static int lqList1 = 2201; //指数
  static int lqList2 = 2202; //球队排名
  static int lqList3 = 2203; //彩种编号
  static int lqList7 = 2207; //指数类型
  static int lqList8 = 2208; //指数公司
  static int lqList9 = 2209; //指数显示
}

mixin Convert {
  Map<int, dynamic> convertJson(Map<int, String> data) {
    Map<int, dynamic> json = {};
    for (var element in data.keys) {
      if (element == ConfigType.zqAlert2 ||
          element == ConfigType.zqAlert5 ||
          element == ConfigType.zqList7 ||
          element == ConfigType.lqAlert2 ||
          element == ConfigType.lqList7) {
        if (data[element]!.isEmpty) {
          json[element] = [];
        } else {
          json[element] =
              data[element]!.split(',').map((e) => int.parse(e)).toList();
        }
      } else {
        json[element] = int.parse(data[element]!);
      }
    }
    return json;
  }
}
